package com.wlcorp.forcazamis.web.action.sample;

import java.io.IOException;
import java.sql.Date;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.wlcorp.forcazamis.business.sample.ISampleService;
import com.wlcorp.forcazamis.domain.baseinfo.Depot;
import com.wlcorp.forcazamis.domain.baseinfo.Factory;
import com.wlcorp.forcazamis.domain.baseinfo.Feature;
import com.wlcorp.forcazamis.domain.baseinfo.SampleType;
import com.wlcorp.forcazamis.domain.sample.Sample;
import com.wlcorp.forcazamis.domain.sample.SampleImage;
import com.wlcorp.forcazamis.domain.sample.SubSample;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.FileFoldPath;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.FileUploadUtil;
import com.wlcorp.forcazamis.sys.util.Pagination;
import com.wlcorp.forcazamis.sys.util.SessionValue;
import com.wlcorp.forcazamis.sys.util.Validator;
import com.wlcorp.forcazamis.sys.util.WebUtil;

public class SampleAction extends DispatchAction {
	public ActionForward sampleManager(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		return mapping.findForward("manager");
	}
	
	public ActionForward getSampleList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Sample sample = new Sample();
		WebUtil.setRequestToObject(request,sample);
		ISampleService service = ContextUtil.getInstance().getSampleService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getSampleCount(sample).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    sample.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
		List<Sample> sampleList = service.getSampleList(sample);
		request.setAttribute("sampleList",sampleList);
		return mapping.findForward("list");
	}
	
	public ActionForward getSample(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Sample sample = new Sample();
		WebUtil.setRequestToObject(request,sample);
		sample = ContextUtil.getInstance().getSampleService().getSampleById(sample.getSampleId());
		if(null == sample){
			sample = new Sample();
		}
		request.setAttribute("sample",sample);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifySample(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff currentStaff = SessionValue.getInstance(request).getStaff();
		Sample sample = new Sample();
		//保存图片跟数据
		String filePath = null;
		SmartUpload smartUpload = FileUploadUtil.getSmartUpload(getServlet(),request,response);
		String sampleId = smartUpload.getRequest().getParameter("sampleId");
		if(!Validator.checkValueIsValid(sampleId)){
			WebUtil.setRequestToObject(smartUpload.getRequest(),sample);
			sample.setCreatedBy(currentStaff);
			sample.setCreatedDate(new Date(Calendar.getInstance().getTimeInMillis()));
		}else{
			sample = ContextUtil.getInstance().getSampleService().getSampleById(Long.valueOf(sampleId));
			WebUtil.setRequestToObject(smartUpload.getRequest(),sample);
			sample.setModifiedBy(currentStaff);
			sample.setModifiedDate(new Date(Calendar.getInstance().getTimeInMillis()));
		}
		try {
			long size = smartUpload.getFiles().getSize();
			if(size > 0){
				String fileName = smartUpload.getFiles().getFile(0).getFileName();
				fileName = WebUtil.gbToUnicode(fileName);
				filePath = FileFoldPath.SAMPLE_IMAGE_FOLD + FileFoldPath.createFileName(fileName);
				smartUpload.getFiles().getFile(0).saveAs(getServlet().getServletContext().getRealPath(filePath));
				sample.setImagePath(filePath);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}
		//如果手工输入样品编号，则以此编号为准
		if(Validator.checkValueIsValid(sample.getTmpSampleCode())){
			sample.setSampleCode(sample.getTmpSampleCode());
		}
		//保存样品
		ContextUtil.getInstance().getSampleService().saveSample(sample);
		if(AppConstant.BASE_BEAN_STATUS_ERROR == sample.getStatus()){
			WebUtil.setAlertMessage(request,sample.getMsg());
		}else{
			sample = ContextUtil.getInstance().getSampleService().getSampleById(sample.getSampleId());
			WebUtil.setAlertMessage(request,"操作成功！");
		}
		request.setAttribute("sample",sample);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifySubSample(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		WebUtil.setResponseNoCache(response);
		SubSample subSample = new SubSample();
		WebUtil.setRequestToObject(request,subSample);
		subSample.setFlag(0);
		subSample.setEcolorDesc(WebUtil.UnicodeToChinese(subSample.getEcolorDesc()));
		subSample.setPlateColor(WebUtil.UnicodeToChinese(subSample.getPlateColor()));
		subSample.setSampleCode(WebUtil.UnicodeToChinese(subSample.getSampleCode()));
		subSample.setSize(WebUtil.UnicodeToChinese(subSample.getSize()));
		subSample.setCcolorDesc(WebUtil.UnicodeToChinese(subSample.getCcolorDesc()));
		subSample.setMemo(WebUtil.UnicodeToChinese(subSample.getMemo()));
		subSample.setBarCode(WebUtil.UnicodeToChinese(subSample.getBarCode()));
		ContextUtil.getInstance().getSampleService().saveSubSample(subSample);
		WebUtil.printText(response,subSample.getSubSampleId().toString());
		return null;
	}
	
	public ActionForward calSerialCode(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		List<SampleType> sampleTypeList = ContextUtil.getInstance().getSampleTypeService().getSampleTypeList(new SampleType());
		List<Feature> featureList = ContextUtil.getInstance().getFeatureService().getFeatureList(new Feature());
		List<Factory> factoryList = ContextUtil.getInstance().getFactoryService().getFactoryList(new Factory());
		request.setAttribute("sampleTypeList",sampleTypeList);
		request.setAttribute("featureList",featureList);
		request.setAttribute("factoryList",factoryList);
		return mapping.findForward("calSerialCode");
	}
	
	public ActionForward getSameTypeNewSerialCode(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		WebUtil.setResponseNoCache(response);
		Sample sample = new Sample();
		WebUtil.setRequestToObject(request,sample);
		Long serialCode = ContextUtil.getInstance().getSampleService().getSameTypeNewSerialCode(sample);
		WebUtil.printText(response,serialCode.toString());
		return null;
	}
	
	public ActionForward saveSampleImage(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		
		SampleImage sampleImage = new SampleImage();
		String filePath = null;
		SmartUpload smartUpload = FileUploadUtil.getSmartUpload(getServlet(),request,response);
		try {
			long size = smartUpload.getFiles().getSize();
			if(size > 0){
				String fileName = smartUpload.getFiles().getFile(0).getFileName();
				fileName = WebUtil.gbToUnicode(fileName);
				filePath = FileFoldPath.SAMPLE_IMAGE_FOLD + FileFoldPath.createFileName(fileName);
				smartUpload.getFiles().getFile(0).saveAs(getServlet().getServletContext().getRealPath(filePath));
				sampleImage.setImagePath(filePath);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}
		
		WebUtil.setRequestToObject(smartUpload.getRequest(),sampleImage);
		ContextUtil.getInstance().getSampleService().saveSampleImage(sampleImage);
		Sample sample = ContextUtil.getInstance().getSampleService().getSampleById(sampleImage.getParentSampleId());
		WebUtil.setAlertMessage(request,"操作成功！");
		request.setAttribute("sample",sample);
		return mapping.findForward("modify");
	}
	
	public ActionForward deleteSampleImage(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		SampleImage sampleImage = new SampleImage();
		WebUtil.setRequestToObject(request,sampleImage);
		ContextUtil.getInstance().getSampleService().deleteSampleImage(sampleImage.getId());
		Sample sample = ContextUtil.getInstance().getSampleService().getSampleById(sampleImage.getParentSampleId());
		request.setAttribute("sample",sample);
		return mapping.findForward("modify");
	}
	
	public ActionForward deleteSubSample(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		WebUtil.setResponseNoCache(response);
		SubSample subSample = new SubSample();
		WebUtil.setRequestToObject(request,subSample);
		subSample = ContextUtil.getInstance().getSampleService().getSubSampleById(subSample.getSubSampleId());
		ContextUtil.getInstance().getSampleService().deleteSubSample(subSample);
		if(AppConstant.BASE_BEAN_STATUS_ERROR == subSample.getStatus()){
			WebUtil.printText(response,subSample.getMsg());
		}else{
			WebUtil.printText(response,"1");
		}
		return null;
	}
	
	public ActionForward searchSample(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		List<SampleType> sampleTypeList = ContextUtil.getInstance().getSampleTypeService().getSampleTypeList(new SampleType());
		List<Feature> featureList = ContextUtil.getInstance().getFeatureService().getFeatureList(new Feature());
		List<Factory> factoryList = ContextUtil.getInstance().getFactoryService().getFactoryList(new Factory());
		request.setAttribute("sampleTypeList",sampleTypeList);
		request.setAttribute("featureList",featureList);
		request.setAttribute("factoryList",factoryList);
		
		return mapping.findForward("search");
	}
	
	public ActionForward getSearchSampleList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Sample sample = new Sample();
		WebUtil.setRequestToObject(request,sample);
		ISampleService service = ContextUtil.getInstance().getSampleService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getSampleCount(sample).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    sample.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
		List<Sample> sampleList = service.getSampleList(sample);
		request.setAttribute("sampleList",sampleList);
		request.setAttribute("displayImage",request.getParameter("displayImage"));
		return mapping.findForward("searchlist");
	}
	
	public ActionForward subSampleAssignSearch(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		List<Depot> depotList = ContextUtil.getInstance().getDepotService().getDepotList(new Depot());
		request.setAttribute("depotList",depotList);
		return mapping.findForward("subSampleAssignSearch");
	}
	
	public ActionForward getSubSampleAssignList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		SubSample subSample = new SubSample();
		WebUtil.setRequestToObject(request,subSample);
		Depot depot=new Depot();
		String depotName=request.getParameter("depotName");
		if(null!=depotName){
			depot.setDepotName(depotName);
		}else{
			depot.setDepotName("");
		} 
		List<Depot> depotList = ContextUtil.getInstance().getDepotService().getDepotList(depot);
		List<SubSample> subSampleList = ContextUtil.getInstance().getSampleService().getSubSampleList(subSample);
		request.setAttribute("depotList",depotList);
		request.setAttribute("subSampleList",subSampleList);
		return mapping.findForward("subSampleAssignlist");
	}
	
	public ActionForward deleteSample(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Sample sample = new Sample();
		WebUtil.setRequestToObject(request,sample);
		ISampleService service = ContextUtil.getInstance().getSampleService();
		sample = service.getSampleById(sample.getSampleId());
		if(null != sample){
			service.deleteSample(sample);
		}
		return getSampleList(mapping,form,request,response);
	}
	
}
